/*
 * handle_bind.c
 *
 *  Created on: 2009-5-5
 *      Author: tian
 */
#include"tcpipsim.h"
#include"string.h"

void handle_bind(msg *pmsg){
	sockmsg retmsg;
	memset(&retmsg,0,sizeof(sockmsg));
	//int i=0;
	struct tcpcb *ptcpcb=get_tcpcb_by_sock(pmsg->pid,pmsg->sock);
	if(ptcpcb==NULL){
		retmsg.mtype=pmsg->sock;
		retmsg.ret=-1;
		if(msgsnd(pmsg->msgqid,&retmsg,sizeof(sockmsg)-sizeof(long),IPC_NOWAIT)<0){
			fprintf(stderr,"can not send message to application layer\n");
			return;
		}
	}
	else{
		retmsg.mtype=pmsg->sock;
		retmsg.ret=0;
		//ptcpcb->sock_addr=pmsg->sock_addr;
		memcpy(&(ptcpcb->sock_addr),pmsg->buf,pmsg->buflen);
		//for(i=0;i<sizeof(struct sockaddr);i++){
		//	*(char*)(&(ptcpcb->sock_addr)+i)=*(pmsg->buf+i);
		//}
		//ptcpcb->sock_addr=*((struct sockaddr *)(pmsg->buf));
		if(msgsnd(ptcpcb->msgqid,&retmsg,sizeof(sockmsg)-sizeof(long),IPC_NOWAIT)<0){
			fprintf(stderr,"can not send message to application layer\n");
			return;
		}
	}
}
